﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch4_general
{
    class Program
    {

        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            Console.WriteLine("a = {0}, b ={1}", a, b);
            //SwapInt.SwapFun(ref a, ref b);
            Swap<int>.SwapFun(ref a, ref b);//与上面一句话的功能相同
            Console.WriteLine("a = {0}, b ={1}", a, b);

            string goodNews = "五一假期要到了";
            string badNews = "4月23号是工作日";
            Console.WriteLine("goodNews = {0}, badNews ={1}", goodNews, badNews);
            //SwapString.SwapFun(ref goodNews, ref badNews);
            Swap<string>.SwapFun(ref goodNews, ref badNews);//与上面一句话的功能相同
            Console.WriteLine("goodNews = {0}, badNews ={1}", goodNews, badNews);

            //实例化的时候要指明具体用那种数据类型指代泛型
            Girlfriend<string> gf1 = new Girlfriend<string>("冰墩墩", 365, "20171010");
            Girlfriend<string> gf2 = new Girlfriend<string>("雪容融", 128, "20181119");
            gf1.show(); gf2.show();
            Swap<Girlfriend<string>>.SwapFun(ref gf1, ref gf2);
            gf1.show(); gf2.show();

            Girlfriend<DateTime> gf3 = 
                new Girlfriend<DateTime>("欢欢", 1024, new DateTime(2020, 1, 1));
            Girlfriend<DateTime> gf4 =
                new Girlfriend<DateTime>("嘻嘻", 512, new DateTime(2000, 1, 1));
            Swap<Girlfriend<DateTime>>.SwapFun(ref gf3, ref gf4);
            gf3.show();gf4.show();

            //Swap<Girlfriend<DateTime>>.SwapFun(ref gf3, ref gf1);
            List<int> list = new List<int>() { 3, 9, 7, 4 };//长度可变
            int[] array = new int[] { 3, 9, 7, 4 };//长度固定为4
            list.Add(5);//在末尾添加元素5
            Console.WriteLine("list[4] = {0}", list[4]);
            list.Insert(2,8);//在位置2添加元素8
            Console.WriteLine("list[2] = {0}", list[2]);
            List<int> l = new List<int>() { 1, 2 };
            list.AddRange(l);//在list末尾加入新的l

            for (int i = 0; i < list.Count; i++)
                Console.Write(list[i] + " ");
            Console.WriteLine("");
            list.Remove(2);//删除元素
            for (int i = 0; i < list.Count; i++)
                Console.Write(list[i] + " ");
            Console.WriteLine("");
            list.RemoveAt(2);//删除指定位置的元素
            for (int i = 0; i < list.Count; i++)
                Console.Write(list[i] + " ");
            Console.WriteLine("");
            //删除大于5的元素,参数是委托，该委托的返回值是bool，有1个int参数
            list.RemoveAll((x)=>(x>5));//删除符合条件的所有元素
            for (int i = 0; i < list.Count; i++)
                Console.Write(list[i] + " ");
            Console.WriteLine("");


            List<string> strList = new List<string>() { "abc", "ertgf", "cccccc", "er" };
            strList.RemoveAll( (y) => y.Length > 3);//谓词（lamda表达式）做参数
            for (int i = 0; i < strList.Count; i++)
                Console.Write(strList[i] + " ");
            Console.WriteLine("");

            int large = list.Find( (x)=>x>2);
            Console.WriteLine("第一个大于2的元素：{0}", large);

            int largeLast = list.FindLast((x) => x > 2);
            Console.WriteLine("最后一个大于2的元素：{0}", largeLast);

            List<int> largeList = list.FindAll((x) => x > 2);
            for (int i = 0; i < largeList.Count; i++)
                Console.Write(largeList[i] + " ");
            Console.WriteLine("");

            //降序排序
            Console.WriteLine("排序");
            list.Sort();
            for (int i = 0; i < list.Count; i++)
                Console.Write(list[i] + " ");
            Console.WriteLine("");


            //Girlfriend的动态数组
            List<Girlfriend<DateTime>> gfList = new List<Girlfriend<DateTime>>();
            gfList.Add(new Girlfriend<DateTime>("贝贝", 128, new DateTime(2010, 3, 4)));
            gfList.Add(new Girlfriend<DateTime>("晶晶", 56, new DateTime(2014, 3, 4)));
            gfList.Add(new Girlfriend<DateTime>("欢欢", 256, new DateTime(2018, 3, 4)));
            gfList.Sort();
            for (int i = 0; i < gfList.Count; i++)
                gfList[i].show();
            Console.WriteLine("");
            Console.Read();
        }
    }
}
